package com.groupbuying.task.service.impl;

import com.groupbuying.task.config.UrlConstants;
import com.groupbuying.task.domain.XxlJobInfoDO;
import com.groupbuying.task.service.XxlJobInfoService;
import com.groupbuying.task.utils.HttpClientUtils;
import com.groupbuying.task.utils.JSONUtils;
import com.groupbuying.task.utils.ObjectUtils;
import com.groupbuying.task.utils.apiresult.ApiResult;
import com.groupbuying.task.utils.apiresult.ApiResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class XxlJobInfoServiceImpl implements XxlJobInfoService {

    public static final Logger logger = LoggerFactory.getLogger(XxlJobInfoServiceImpl.class);

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Override
    public ApiResult add(XxlJobInfoDO xxlJobInfo) {
        try {
            // http调接口启动
            String result = httpClientUtils.doPost(UrlConstants.TASK_ADD, xxlJobInfo);
            // 调用立即启动接口
            Map<String, Object> map = JSONUtils.jsonToMap(result);
            if (map.get("code") == null || !map.get("code").toString().equals("200")) {
                logger.info("定时任务添加失败,{},{}", result, map);
                return ApiResult.failure("定时任务添加失败");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("id",Integer.valueOf(map.get("content").toString()));
            String startResult = httpClientUtils.doGet(UrlConstants.TASK_START,param);
            Map<String, Object> startMap = JSONUtils.jsonToMap(startResult);
            if (map.get("code") == null || !map.get("code").toString().equals("200")) {
                logger.info("定时任务启动失败,{},{}", startResult, startMap);
                return ApiResult.failure("定时任务启动失败");
            }
            return ApiResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResult.failure();
    }

    @Override
    public ApiResult update(XxlJobInfoDO xxlJobInfo) {
        try {
            String result = httpClientUtils.doPost(UrlConstants.TASK_UPDATE, xxlJobInfo);
            Map<String, Object> map = JSONUtils.jsonToMap(result);
            if (map.get("code") == null || !map.get("code").toString().equals("200")) {
                logger.info("定时任务修改失败,{},{}", result, map);
                return ApiResult.failure("定时任务修改失败");
            }

            return ApiResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ApiResult.failure();
    }

    @Override
    public ApiResult updateByExecutorParam(XxlJobInfoDO xxlJobInfo) {
        try {
            String result = httpClientUtils.doPost(UrlConstants.TASK_UPDATE_RED_PACKAGE, xxlJobInfo);
            Map<String, Object> map = JSONUtils.jsonToMap(result);
            if (map.get("code") == null || !map.get("code").toString().equals("200")) {
                logger.info("定时任务修改失败,{},{}", result, map);
                return ApiResult.failure("定时任务修改失败");
            }

            // 调用立即启动接口
//            Map<String,Object> param = new HashMap<>();
//            param.put("id",Integer.valueOf(map.get("content").toString()));
//            String startResult = httpClientUtils.doGet(UrlConstants.TASK_START,param);
//            Map<String, Object> startMap = JSONUtils.jsonToMap(startResult);
//            if (map.get("code") == null || !map.get("code").toString().equals("200")) {
//                logger.info("定时任务启动失败,{},{}", startResult, startMap);
//                return ApiResult.failure("定时任务启动失败");
//            }
            return ApiResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ApiResult.failure();
    }

    @Override
    public ApiResult remove(Integer id) {
        try {
            String result = httpClientUtils.doGetForTask(UrlConstants.TASK_DELETE, id);
            Map<String, Object> map = JSONUtils.jsonToMap(result);

            logger.info("定时任务删除:{}", JSONUtils.beanToJson(result));

            return ApiResultCode.OK.code().equals(map.get("code")) ? ApiResult.success() : ApiResult.failure();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ApiResult.failure();
    }
}

